package junit.admin;

import com.jfinal.aop.Inject;
import io.jboot.test.MockMvcResult;
import io.jpress.module.page.model.SinglePage;
import io.jpress.module.page.service.SinglePageService;
import junit.base.AdminBaseTester;
import org.junit.Assert;
import org.junit.Test;

import java.util.Map;

public class AdminPageTester extends AdminBaseTester {

    @Inject
    private SinglePageService pageService;

    @Test
    public void startPageTestCases() {

        //用户登录，默认账号 admin 123456
        doLogin();

        //发布新的页面
        testAddPage();

        //更新页面信息
        testUpdatePage();

        //删除页面
        testDeletePage();

    }


    /**
     * 测试发布新页面
     */
    public void testAddPage() {
        SinglePage page = new SinglePage();
        page.setTitle("test title");

        Map<String, Object> paras = buildParas(page, "page");
        MockMvcResult mvcResult = postWithCsrfToken("/admin/page/doWriteSave", paras);
        Assert.assertTrue(mvcResult.getContent().contains("内容不能为空"));


        page.setContent("test content");
        paras = buildParas(page, "page");
        mvcResult = postWithCsrfToken("/admin/page/doWriteSave", paras);

        Assert.assertTrue(mvcResult.getStatus() == 200);
        Assert.assertTrue(mvcResult.getContentAsJSONObject().get("state").equals("ok"));

        Long pageId = mvcResult.getContentAsJSONObject().getLong("id");
        putContainerData("pageId", pageId);

        mvcResult.printResult();
    }


    /**
     * 测试更新页面
     */
    public void testUpdatePage() {

        SinglePage page = new SinglePage();
        page.setTitle("test title");
        page.setContent("new test content");
        page.setId((Long) getContainerData("pageId"));

        Map<String, Object> paras = buildParas(page, "page");

        MockMvcResult mvcResult = postWithCsrfToken("/admin/page/doWriteSave", paras);
        mvcResult.printResult();

        Assert.assertTrue(mvcResult.getStatus() == 200);
        Assert.assertTrue(mvcResult.getContentAsJSONObject().get("state").equals("ok"));

        Long pageId = mvcResult.getContentAsJSONObject().getLong("id");
        putContainerData("pageId", pageId);

        final SinglePage byId = pageService.findById(pageId);
        Assert.assertEquals(byId.getContent(), page.getContent());
    }


    /**
     * 测试删除页面
     */
    public void testDeletePage() {
        Long pageId = (Long) getContainerData("pageId");
        MockMvcResult mvcResult = getWithCsrfToken("/admin/page/doDel/" + pageId);
        mvcResult.printResult();

        Assert.assertTrue(mvcResult.getStatus() == 200);
    }



}
